package feng.demo.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class PackJson {
    static Logger logger=LoggerFactory.getLogger(PackJson.class);
    static ObjectMapper objectMapper=new ObjectMapper();
    public static String pack(int code,String msg,Object data){
        Map<String,Object> map=new HashMap<>();
        map.put("code",code);
        map.put("msg",msg);
        map.put("data",data);
        String s="";
        try {
            //把map转成json字符串
            s = objectMapper.writeValueAsString(map);
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        logger.debug("json="+s);
        return s;
    }



    public static String packJson(int code,String msg,String data){
        String strJson="{\"code\":\""+code+"\",\"msg\":\""+msg+"\",\"data\":\""+data+"\"}";
        return strJson;
    }
    public static String packJson2(int code,String msg,String data){
        String strJson="{\"code\":\""+code+"\",\"msg\":\""+msg+"\",\"data\":{\"token\":\""+data+"\"}}";
        return strJson;
    }
}
